import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

class LInput extends StatefulWidget {
  /// 输入框的内容
  final String? value;

  /// 控制器
  final TextEditingController? controller;

  /// 焦点控制器
  final FocusNode? focusNode;

  /// 内容颜色
  final Color? color;

  /// 字体大小
  final double? fontSize;

  /// 提示文字
  final String? hintText;

  /// 提示文字颜色
  final Color? hintColor;

  /// 提示文字字号
  final double? hintFontSize;

  /// 输入框内容改变回调
  final void Function(String)? onChange;

  /// 输入框失去焦点回调
  final void Function(String)? onBlur;

  /// 最大行数
  final int? maxLines;

  /// 最大长度
  final int? maxLength;

  /// 行高
  final double? lineHeight;

  /// 后缀图标
  final Widget? suffixIcon;

  /// 是否是密码模式
  final bool? isPassword;

  /// 是否禁用
  final bool? disabled;

  /// 紧缩模式 文字上下方没有间距 但如果设置了`suffixIcon`则不生效
  final bool? isDense;

  final List<RegExp>? formater;

  /// 是否是数字键盘
  final bool? numberKeyboard;

  ///  输入框内边距
  final List<double>? padding;

  /// 内容是否可以选择 true 可以 false 不可以
  final bool? enableSelection;

  /// 事件按钮类型
  final TextInputAction? textInputAction;

  /// 事件按钮点击
  final Function(String)? onSubmitted;
  const LInput(
      {this.color,
      this.value,
      this.controller,
      this.focusNode,
      this.fontSize,
      this.hintText,
      this.hintColor,
      this.hintFontSize,
      this.onChange,
      this.maxLines,
      this.lineHeight,
      this.maxLength,
      this.suffixIcon,
      this.isPassword,
      this.disabled,
      this.isDense,
      this.onBlur,
      this.formater,
      this.numberKeyboard,
      this.padding,
      this.textInputAction,
      this.onSubmitted,
      this.enableSelection,
      Key? key})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _LInput();
}

class _LInput extends State<LInput> {
  late TextEditingController controller;
  late FocusNode focusNode;
  @override
  void initState() {
    super.initState();
    controller = widget.controller ?? TextEditingController();
    if (widget.value?.isNotEmpty == true) {
      controller.text = widget.value!;
    }
    focusNode = widget.focusNode ?? FocusNode();
    focusNode.addListener(_handleFocusNodeChange);
  }

  _handleFocusNodeChange() {
    if (!focusNode.hasFocus && widget.onBlur != null) {
      widget.onBlur!(controller.text);
    }
  }

  @override
  void didUpdateWidget(LInput oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.value != widget.value && Platform.isAndroid) {
      var s = controller.selection.extentOffset;
      controller.text = widget.value ?? '';
      if (s > controller.text.length) {
        s = controller.text.length;
      }
      controller.selection =
          TextSelection.fromPosition(TextPosition(offset: s));
    }
  }

  @override
  void dispose() {
    controller.dispose();
    focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var usePadding = findTRBL<double>(widget.padding ?? [0]);
    return TextField(
      enableInteractiveSelection: widget.enableSelection == true,
      textInputAction: widget.textInputAction,
      onSubmitted: widget.onSubmitted,
      keyboardType: widget.numberKeyboard == true ? TextInputType.number : null,
      inputFormatters: widget.formater?.isNotEmpty == true
          ? widget.formater!
              .map((e) => FilteringTextInputFormatter.allow(e))
              .toList()
          : null,
      scrollPadding: EdgeInsets.zero,
      maxLines: widget.maxLines ?? 1,
      controller: controller,
      focusNode: focusNode,
      maxLength: widget.maxLength,
      obscureText: widget.isPassword == true,
      style: TextStyle(
          fontSize: widget.fontSize ?? LStyle.fontSize,
          height: widget.lineHeight,
          color: widget.color),
      decoration: InputDecoration(
          isDense: widget.isDense,
          counterText: '',
          disabledBorder: const OutlineInputBorder(
              borderSide: BorderSide(width: 0, color: Colors.transparent)),
          enabledBorder: const OutlineInputBorder(
              borderSide: BorderSide(width: 0, color: Colors.transparent)),
          focusedBorder: const OutlineInputBorder(
              borderSide: BorderSide(width: 0, color: Colors.transparent)),
          border: const OutlineInputBorder(
              borderSide: BorderSide(width: 0, color: Colors.transparent)),
          contentPadding: EdgeInsets.only(
            top: usePadding[0] ?? 0,
            right: usePadding[1] ?? 0,
            bottom: usePadding[2] ?? 0,
            left: usePadding[3] ?? 0,
          ),
          suffixIcon: widget.suffixIcon,
          hintText: widget.hintText,
          enabled: widget.disabled != true,
          hintStyle: TextStyle(
              color: widget.hintColor,
              fontSize:
                  widget.hintFontSize ?? widget.fontSize ?? LStyle.fontSize)),
      onChanged: widget.onChange,
    );
  }
}
